/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.montoroTurismo.beans;

import java.sql.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import org.montoroTurismo.dao.AccesoBD;
import org.primefaces.context.RequestContext;

/**
 *
 * @author ajdiaz
 */
public class OffersBean {

    /**
     * Creates a new instance of OffersBean
     */
    private Part imagenOffer;
    private int id;
    private String titulo;
    private String dateEnd;
    private String descripcion;
    private final HttpServletRequest httpServletRequest;
    private final FacesContext faceContext;
    private OffersBean offertable;

    public OffersBean() {
        faceContext = FacesContext.getCurrentInstance();
        httpServletRequest = (HttpServletRequest) faceContext.getExternalContext().getRequest();
        if (("modificar").equals(httpServletRequest.getSession().getAttribute("accion"))) {
            offertable = (OffersBean) httpServletRequest.getSession().getAttribute("offerTableForm");
            this.setId(offertable.getId());
            this.setDateEnd(offertable.getDateEnd());
            this.setTitulo(offertable.getTitulo());
            this.setDescripcion(offertable.getDescripcion());
            System.out.println("DATOS modificar:  " + descripcion);

        }
    }

    public OffersBean(int id, String titulo, String dateEnd, String descripcion) {
        faceContext = FacesContext.getCurrentInstance();
        httpServletRequest = (HttpServletRequest) faceContext.getExternalContext().getRequest();
        this.id = id;
        this.titulo = titulo;
        this.dateEnd = dateEnd;
        this.descripcion = descripcion;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitulo() {
        return titulo;
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }

    public String getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(String dateEnd) {
        this.dateEnd = dateEnd;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public Part getImagenOffer() {
        return imagenOffer;
    }

    public void setImagenOffer(Part imagenOffer) {
        this.imagenOffer = imagenOffer;
    }

    public String saveOffer() {
        String succes = "";
        FacesMessage msg = null;
        try {
            AccesoBD db = new AccesoBD();
            String username = httpServletRequest.getSession().getAttribute("sessionUsuario").toString();
            if (("modificar").equals(httpServletRequest.getSession().getAttribute("accion"))) {
                httpServletRequest.getSession().setAttribute("accion", "");
                db.updateOffer(this, username);
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Oferta modificada!", "La ofertase modifico correctamente!");
                succes = "succes";


            } else {

                db.insertOffer(this, username);
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Oferta Lanzada!", "La oferta se lanzo correctamente");
                succes = "succes";

            }


            db.close();

        } catch (Exception ex) {
            Logger.getLogger(OffersBean.class.getName()).log(Level.SEVERE, null, ex);
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Error!", "Ha habido un error");
        }

        FacesContext.getCurrentInstance().addMessage(null, msg);

        return succes;
    }
}
